package com.handpay.zztong.hp.adapter;

import android.content.Context;

import android.graphics.drawable.Drawable;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.Checkable;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.handpay.framework.utils.CommonUtils;
import com.handpay.zztong.hp.R;

/**
 * Created by jmshuai on 2014/8/21.
 */
public class SwiperModelAdapter extends BaseAdapter {
	private String[] deviceNames;
	private Drawable[] mDrawables;
	private Context mContext;

	public SwiperModelAdapter(Context context, String[] devices, Drawable[] drawables) {
		super();
		mContext = context;
		mDrawables=drawables;
		deviceNames = devices;
	}

	@Override
	public int getCount() {
		return deviceNames.length;
	}

	@Override
	public Object getItem(int i) {
		return deviceNames[i];
	}

	@Override
	public long getItemId(int i) {
		return i;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		SwiperModelItemView itemView;
		/**
		 * convertView会不会导致check的状态不对？
		 */
		if (null != convertView) {
			itemView = (SwiperModelItemView) convertView;
		} else {
			itemView = new SwiperModelItemView(mContext);
		}
		if (null != parent && parent instanceof ListView) {
			itemView.setChecked(((ListView) parent).isItemChecked(position));
		}

		itemView.setSwiperInfo(deviceNames[position], mDrawables[position]);

		return itemView;
	}

	/**
	 * Checkable,是为了ListView的单选起作用
	 */
	public class SwiperModelItemView extends LinearLayout implements Checkable {
		private ImageView mIv;
		private TextView mName;
		private RadioButton mRB;

		public SwiperModelItemView(Context c) {
			super(c);
			this.setLayoutParams(new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
			// this.setClickable(true);
			this.setFocusable(false);
			this.setOrientation(HORIZONTAL);
			this.setGravity(Gravity.CENTER_VERTICAL);
			mIv = new ImageView(c);
			float size = c.getResources().getDimension(R.dimen.swiper_pic_dp);
			LinearLayout.LayoutParams params = new LinearLayout.LayoutParams((int) size, (int) size);
			params.setMargins(CommonUtils.dp2px(10), 0, CommonUtils.dp2px(10), 0);
			this.addView(mIv, params);
			mName = new TextView(c);
			mName.setTextColor(c.getResources().getColor(R.color.normal_text));

			mName.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT, 1));
			mName.setGravity(Gravity.CENTER_VERTICAL);
			mName.setFocusable(false);
			mName.setTextSize(TypedValue.COMPLEX_UNIT_PX,c.getResources().getDimensionPixelSize(R.dimen.normal_text_size));
			this.addView(mName);
			mRB = new RadioButton(c);
			mRB.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
			mRB.setButtonDrawable(c.getResources().getDrawable(R.drawable.radio_btn));
			mRB.setClickable(false);
			mRB.setFocusable(false);
			mRB.clearFocus();
			this.addView(mRB);
		}

		public void setSwiperInfo(String text, Drawable swipePic) {
			mName.setText(text);
			mIv.setImageDrawable(swipePic);
		}

		@Override
		public void setChecked(boolean b) {
			// HPLog.i("shuai", "setChecked" + b);
			mRB.setChecked(b);
		}

		@Override
		public boolean isChecked() {
			return mRB.isChecked();
		}

		@Override
		public void toggle() {
			// HPLog.i("shuai", "toggle");
			mRB.toggle();
		}

		@Override
		public boolean hasFocusable() {
			// HPLog.i("shuai", super.hasFocusable());
			// 这里必须放回false，才会触发onItemClick事件
			return false;
		}

	}
}
